package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Storey;
import java.util.List;

import com.njit.WeStudy.dataobject.StoreyView;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface StoreyMapper {
    @Delete({
        "delete from storey",
        "where storey_id = #{storeyId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer storeyId);

    @Insert({
        "insert into storey ( user_id, ",
        "invitation_id, create_time, ",
        "content, support_number, ",
        "comment_number)",
        "values ( #{userId,jdbcType=INTEGER}, ",
        "#{invitationId,jdbcType=INTEGER}, now(), ",
        "#{content,jdbcType=VARCHAR}, 0, 0)"
    })
    int insert(Storey record);

    @Select({
        "select",
        "storey_id, user_id, invitation_id, create_time, content, support_number, comment_number",
        "from storey",
        "where storey_id = #{storeyId,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="storey_id", property="storeyId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="support_number", property="supportNumber", jdbcType=JdbcType.INTEGER),
        @Result(column="comment_number", property="commentNumber", jdbcType=JdbcType.INTEGER)
    })
    Storey selectByPrimaryKey(Integer storeyId);

    @Select({
        "select",
        "storey_id, user_id, invitation_id, create_time, content, support_number, comment_number",
        "from storey"
    })
    @ResultMap("ResultMap")
    List<Storey> selectAll();

    //查找层主内容视图
    @Select({
            "select",
            "storey_id, storey.user_id, invitation_id, create_time, content, support_number, comment_number,user_name,head_image",
            "from storey,user where storey.user_id = user.user_id"
    })
    @Results(id = "ResultViewMap",value = {
            @Result(column="storey_id", property="storeyId", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
            @Result(column="support_number", property="supportNumber", jdbcType=JdbcType.INTEGER),
            @Result(column="comment_number", property="commentNumber", jdbcType=JdbcType.INTEGER),
            @Result(column = "user_name",property = "userName",jdbcType=JdbcType.VARCHAR),
            @Result(column = "head_image",property = "headImage",jdbcType=JdbcType.VARCHAR)
    })
    List<StoreyView> selectAllView();

    //特意提供的包含用户名头像的查询方法
    @SelectProvider(type = QueryBuilder.class,method = "selectViewByInvitationId")
    @ResultMap("ResultViewMap")
    List<StoreyView> selectViewByInvitationId(@Param("invitationId")int invitationId);
    //一般查询跟帖数量(加.size())
    @SelectProvider(type = QueryBuilder.class,method = "storeyQueryByInvitationId")
    @ResultMap("ResultMap")
    List<Storey> storeyQueryByInvitationId(@Param("invitationId")int invitationId);
    //查询当前用户的跟帖
    @SelectProvider(type = QueryBuilder.class,method = "storeyQueryByUserId")
    @ResultMap("ResultMap")
    List<Storey> storeyQueryByUserId(@Param("userId")int userId);
    //查询当前用户对当前帖子的评论
    @SelectProvider(type = QueryBuilder.class,method = "queryMyselfStoreys")
    @ResultMap("ResultMap")
    List<Storey> queryMyselfStoreys(@Param("userId")int userId,@Param("invitationId")int invitationId);

    //根据id修改当前跟帖的support或者comment数目
    @UpdateProvider(type = UpdateBuilder.class,method = "updateNumById")
    int updateNumById(@Param("numType")String numType,@Param("num")int num,@Param("storeyId")int storeyId);


    @Update({
        "update storey",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "invitation_id = #{invitationId,jdbcType=INTEGER},",
          "content = #{content,jdbcType=VARCHAR},",
          "support_number = #{supportNumber,jdbcType=INTEGER},",
          "comment_number = #{commentNumber,jdbcType=INTEGER}",
        "where storey_id = #{storeyId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Storey record);
}